#include <stdio.h>
#include <unistd.h>
#include <crypt.h>
#include <fcntl.h>

static unsigned int pw_zrand(void)
{
    int fd;
    int ret; 
    if( ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) == -1) ) {
       return (unsigned int) rand();
    }

    if (read(fd, &ret, sizeof ret) != (ssize_t) sizeof ret) {
        close(fd);
        return (unsigned int) rand();
    }
    close(fd);
    return (unsigned int) ret; 
}

int main(int argc, char *argv[]) {

 if(argc < 2){
   printf("Blowfish password generator for Pure-FTPd V1.0\n\n");
   
   printf("Usage: %s <password>\n", argv[0]);
   return 1;
 }

  const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  char salt[] = "$2a$07$0000000000000000000000";        
  char *cryptedpass;

  int c = 28;

  do {
      c--;
      salt[c] = seedchars[pw_zrand() & 63];
     } while (c > 7);

  cryptedpass = crypt(argv[1], salt);

  puts(cryptedpass);
  return 0;
}